<template>
	<uni-card :is-shadow="false" is-full>
		<text class="uni-h6">wifi插件</text>
	</uni-card>
	<uni-section title="参数" type="line" padding>
		<uni-easyinput v-model="ssid" placeholder="用户名"></uni-easyinput>
		<uni-easyinput v-model="password" placeholder="密码"></uni-easyinput>
	
	</uni-section>
	<uni-section title="日志" type="line" padding>
	
		<text class="uni-h6">{{logTxt}}</text>
	</uni-section>
	
	<uni-section title="操作" type="line">
		<button @click="openWifiEnabled" type="primary" class="mt-1 mx-1">打开WiFi</button>
		<button @click="connectWifi" type="primary" class="mt-1 mx-1">连接wifi</button>
		<button @click="disconnectWifi" type="primary" class="mt-1 mx-1">断开wifi</button>
	</uni-section>
</template>

<script>
	export default {
		data() {
			return {
				ssid: 'huo15',
				password: 'huo15.com',
				huo15Sdk: null,
				logTxt:'',
				TAG:"WifiModule",
			}
		},
		onLoad() {
			this.huo15Sdk = uni.requireNativePlugin('Huo15-WifiModule')
			const globalEvent = uni.requireNativePlugin('globalEvent')
		
			this.log(this.huo15Sdk, 'sdk instance: ')
			this.huo15Sdk.requestPermissions()
			globalEvent.addEventListener('huo15_notify', (e) => {
				this.log(JSON.stringify(e))
				if(e.is_connect!==undefined){
					this.logTxt='WiFi连接是否成功： '+e.is_connect
				}
				if(e.msg!==undefined){
					this.logTxt=e.msg
				}
				
			});

		},
		methods: {
			disconnectWifi(){
				this.log('disconnectWifi')
				this.huo15Sdk.disconnectWifi()
			},
			connectWifi() {
				this.log('connectWifi')
				this.huo15Sdk.connectWifi({
					ssid: this.ssid,
					password: this.password,
				})
			},
			openWifiEnabled() {
				this.log('openWifiEnabled')
				this.huo15Sdk.openWifiEnabled()
			},
			log(msg, t = '') {
				console.log(this.TAG + ":" + t, msg);
			},

		},
		onUnload() {
			this.huo15Sdk.unregisterWifiReceiver();
		},
	}
</script>

<style lang="scss" scoped>
	.mt-1 {
		margin-top: 10rpx;
	}

	.mx-1 {
		margin-left: 10rpx;
		margin-right: 10rpx;
	}
</style>